import * as vscode from 'vscode';
import { LoginService } from '../../src/services/loginService';

/**
 * 检查用户登录状态，如果未登录则显示提示并启动登录流程
 * @param context VSCode扩展上下文
 * @returns 如果已登录返回true，否则返回false
 */
export async function checkLoginStatus(context: vscode.ExtensionContext): Promise<boolean> {
    const loginService = LoginService.getInstance(context);
    const isLoggedIn = await loginService.isLoggedIn();
    
    if (!isLoggedIn) {
        const choice = await vscode.window.showWarningMessage(
            '此功能需要登录后才能使用',
            '立即登录',
            '取消'
        );
        
        if (choice === '立即登录') {
            await loginService.startLogin({
                onSuccess: (userInfo) => {
                    vscode.window.showInformationMessage(`登录成功，欢迎 ${userInfo.cnName}！`);
                },
                onFailure: (reason, message) => {
                    vscode.window.showErrorMessage(`登录失败: ${message}`);
                }
            });
        }
        
        return false;
    }
    
    return true;
}